<template>
	<div class="system-dictionary-container">
		<el-dialog title="修改字典" v-model="isShowDialog" >
			<el-form ref="editDictionaryFormRef" :model="formData" :rules="formRules" label-width="80px">
				<el-row :gutter="35">
					<el-col>
						<el-form-item label="上级字典">
							<el-cascader
								placeholder="请选择上级字典（可选填）"
								v-model="formData.parentId"
								:options="dictionarys"
								:props="{expandTrigger: 'hover', emitPath: false, checkStrictly: true, value: 'id', label: 'dictName', children: 'children' }"
								class="w100"
								clearable>
							</el-cascader>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="字典类型" prop="dictType">
							<el-radio v-model="formData.dictType" label="catalog" clearable>目 录</el-radio>
							<el-radio v-model="formData.dictType" label="dictionary" clearable>字 典</el-radio>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="字典排序" prop="orderNum">
							<el-input-number v-model="formData.orderNum" :min="0" :max="999" clearable class="w100"></el-input-number>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="字典名称" prop="dictName">
							<el-input v-model="formData.dictName" placeholder="请输入字典名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" v-if="formData.dictType == 'catalog'">
						<el-form-item label="字典键值" prop="dictLabel">
							<el-input v-model="formData.dictLabel" placeholder="请输入字典键值" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" v-if="formData.dictType == 'dictionary'">
						<el-form-item label="字典键值" prop="dictValue">
							<el-input v-model="formData.dictValue" placeholder="请输入字典键值" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" v-if="formData.dictType == 'dictionary'">
						<el-form-item label="是否默认" prop="isDefault">
							<el-radio v-model="formData.isDefault" label="Y" clearable>是</el-radio>
							<el-radio v-model="formData.isDefault" label="N" clearable>否</el-radio>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
						<el-form-item label="字典状态" prop="status">
							<el-radio v-model="formData.status" label="0" clearable>正常</el-radio>
							<el-radio v-model="formData.status" label="1" clearable>停用</el-radio>
							<el-radio v-model="formData.status" label="2" clearable>删除</el-radio>
						</el-form-item>
					</el-col>
					<el-col>
						<el-form-item label="备注">
							<el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" clearable></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel">取 消</el-button>
					<el-button type="primary" @click="onSubmit">保 存</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { getDictionaryList, editDictionary } from '/@/api/system/dictionary/index';
import { lineToTreeData } from '/@/utils/commonFunction';
export default {
	name: 'dictionary/edit',
	setup(props, context) {
		const editDictionaryFormRef = ref();

		const state = reactive({
			isShowDialog: false,
			dictionarys: [] as any,
			formData: {},
			formRules : {
				dictType: { required: true, message: '字典类型不能为空', trigger: 'change' },
				dictName: { required: true, message: '字典名称不能为空', trigger: 'blur' },
				isDefault: { required: true, message: '是否默认不能为空', trigger: 'change' },
				dictValue: { required: true, message: '字典键值不能为空', trigger: 'blur' },
				dictLabel: { required: true, message: '字典键值不能为空', trigger: 'blur' },
				status: { required: true, message: '字典状态不能为空', trigger: 'change' },
				orderNum: { required: true, message: '字典排序不能为空', trigger: 'blur' }
			}
		});
		//初始化上级级联
		const loadDictionarys = async () => {
			await getDictionaryList({dictType: 'catalog'}).then((res) => {
				if(res.code == 0){
					state.dictionarys = lineToTreeData(res.data);
				}
			});
		}
		// 打开弹窗
		const openDialog = (row: any) => {
			state.isShowDialog = true;
			if(row){
				state.formData = Object.assign({}, row);
			}
			loadDictionarys();
		};
		// 关闭弹窗
		const closeDialog = (row?: object) => {
			state.isShowDialog = false;
			for(var key in state.formData) {
				state.formData[key] = undefined;
			}
			editDictionaryFormRef.value.resetFields();
		};
		// 取消
		const onCancel = () => {
			closeDialog();
		};
		// 保存
		const onSubmit = () => {
			editDictionaryFormRef.value.validate(valid => {
				if(valid){
					let params = {
						id: state.formData.id,
						parentId: state.formData.parentId,
						dictName: state.formData.dictName,
						dictType: state.formData.dictType,
						dictLabel: state.formData.dictLabel,
						dictValue: state.formData.dictValue,
						isDefault: state.formData.isDefault,
						status: state.formData.status,
						orderNum: state.formData.orderNum,
						remark: state.formData.remark
					}
					editDictionary(params).then((res: any) => {
						if(res.code === 0){
							ElMessage.success('保存成功');
							context.emit('submit');
							closeDialog(); // 关闭弹窗
						}
					});
				}
			});
		};
		
		return {
			editDictionaryFormRef,
			openDialog,
			closeDialog,
			onCancel,
			onSubmit,
			...toRefs(state),
		};
	},
};
</script>
